import {Component, OnInit} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {FormBuilder, Validators, FormControl} from '@angular/forms';
import {Command} from '../../../../shared/toolbar.service';
import {BladeContainerBase} from '../../../../shared/blade/blade-container-base';
import {Blade} from '../../../../shared/blade/blade.model';
import {BladeData} from '../../../../shared/blade/blade-data';
import {DialogResult} from '../../../../shared/dialog.service';
// import { TagMark } from "../../../../entity/TagMark.model";
// import { TubStatusExper } from "../../../../entity/tub-status-exper.model";


// import { TubExper } from "../../../../entity/staus-exper.model";
// import { TurbStatus } from "../../../../entity/turbstatus.model";
// import { StausExperService } from "../../../../service/staus-exper.service";
import {TurbStatusService} from '../../../../service/turbstatus.service';
// import { TurbStatusVOList } from "../../../../entity/turbStatusVOList.model";
import {StausExperEditComponent} from '../staus-exper-edit/staus-exper-edit.component';
import {TurbStatus} from '../../../../entity/turbstatus.model';
import {TurbStatusExpr, TurbStatusExprData, TagMark} from '../../../../entity/turbstatus-expr.model';
import {TurbStatusExprService} from '../../../../service/turbstatus_expr.service';

@Component({
  selector: 'app-staus-exper-list',
  templateUrl: './staus-exper-list.component.html',
  styleUrls: ['./staus-exper-list.component.css']
})
export class StausExperListComponent extends BladeContainerBase<TurbStatusExpr> {

  /**
   *风机状态表达式
   *
   * @type {TurbStatusExpr[]}
   * @memberof StausExperListComponent
   */
  turbStatusExprs: TurbStatusExpr[] = [];

  /**
   * 测点标识
   *
   * @type {TagMark[]}
   * @memberof StausExperListComponent
   */
  tagMarks: TagMark[] = [];


  constructor(
    private fb: FormBuilder,
    private turbStatusExprService: TurbStatusExprService,
    private turbastatusService: TurbStatusService) {
    super(fb);
  }

  ngOnInit(): void {
    this.commands = [
      new Command('edit', '编辑', 'fa fa-edit', 'setofkey:update', blade => this.exeEdit(blade), blade => this.canExeEdit(blade)),
    ];
    super.ngOnInit();

  }

  /**
   * 初始化数据
   *
   *
   * @memberof FarmListComponent
   */
  initData(): void {
    let turbStatusExprsGet = this.turbStatusExprService.getTurbStatusExprsBymodelId(this.currentEntity.modelId);
    let turbStatusGet = this.turbastatusService.getTurbStatuies();
    Promise.all([turbStatusExprsGet, turbStatusGet])
      .then(res => {
        this.turbStatusExprs = [];
        let turbStatuies: TurbStatus[] = [];
        if (res[1]) {
          turbStatuies = res[1].filter(turbStatus => turbStatus.deviceCategoryId == this.parentEntity.deviceCategoryId);
        }
        ;
        let turbStatusExprs = res[0];
        this.tagMarks = turbStatusExprs.tagStatusVOList;
        turbStatuies.forEach(status => {
          let turbStatusExpr = turbStatusExprs.turbStatusVOList.find(o => o.statusId == status.statusId)
          let expr = turbStatusExpr ? turbStatusExpr.expr : '';
          // if(this.turbStatusExprs.length>=turbStatuies.length){
          //   this.turbStatusExprs.length = 0;
          //   this.turbStatusExprs.push(new TurbStatusExpr(1, status.statusId, expr, status.statusName, status.statusColor));
          // }else{
          this.turbStatusExprs.push(new TurbStatusExpr(this.currentEntity.modelId, status.statusId, expr, status.statusName, status.statusColor));
          // }
        });

        this.bladeData.isLoading = false;
      })
      .catch(err => {
        this.bladeData.error = err;
      });
  }


  //编辑
  exeEdit(blend: Blade): void {
    if (this.currentEntity != undefined) {
      let data: BladeData = new BladeData();
      data.id = 'stausExperEdit';
      data.title = this.currentEntity.statusName;
      let editKeyBlade = this.bladeService.createBlade(StausExperEditComponent, data, false, this.currentEntity, this.tagMarks);
      this.currentBlade.bladeService.showBlade(editKeyBlade, this.currentBlade);
    }
  }

  canExeEdit(blend?: Blade): boolean {
    return this.currentEntity != null;
  }

  /**
   * 单选
   *
   * @param {any} event
   * @param {any} key
   *
   * @memberof FarmListComponent
   */
  onItemCheack(event: HTMLInputElement, turbStatusExpr: TurbStatusExpr) {
    turbStatusExpr.isChecked = event.checked;
  }

  /**
   * 全选/全不选
   *
   * @param {any} event
   *
   * @memberof FarmListComponent
   */
  onAllCheack(event: HTMLInputElement) {
    if (this.turbStatusExprs.length > 0) {
      for (let i = 0; i < this.turbStatusExprs.length; i++) {
        this.turbStatusExprs[i].isChecked = event.checked;
      }
    }
  }

  onSeletedChange(turbStatusExpr: TurbStatusExpr) {
    if (this.currentBlade.childrenCanDeactivate()) {
      this.currentEntity = turbStatusExpr;
    }
  }
}


// WEBPACK FOOTER //
// ./src/app/module/basic/staus-exper/staus-exper-list/staus-exper-list.component.ts
